import modelExtend from 'dva-model-extend'
import { create, remove, update } from '../services/autoreply'
import * as autoreplysService from '../services/autoreplys'
import { pageModel } from './common'
import { config } from '../utils'

const { query } = autoreplysService
const { prefix } = config

export default modelExtend(pageModel, {
  namespace: 'autoreply',

  state: {
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
    isMotion: localStorage.getItem(`${prefix}autoreplyIsMotion`) === 'true',
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/autoreply') {
        dispatch({
          type: 'query',
          payload: location.query,
        })
      }
    })
    },
  },

  effects: {

  *query ({ payload = {} }, { call, put }) {
  const data = yield call(query, payload)
  if (data) {
    yield put({
      type: 'querySuccess',
      payload: {
        list: data.data,
        pagination: {
          current: Number(payload.page) || 1,
          pageSize: Number(payload.pageSize) || 10,
          total: data.total,
        },
      },
    })
  }
},

*'delete' ({ payload }, { call, put, select }) {
  const data = yield call(remove, { id: payload })
  const { selectedRowKeys } = yield select(_ => _.autoreply)
  if (data.success) {
    yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
    yield put({ type: 'query' })
  } else {
    throw data
  }
},

*'multiDelete' ({ payload }, { call, put }) {
  const data = yield call(autoreplysService.remove, payload)
  if (data.success) {
    yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
    yield put({ type: 'query' })
  } else {
    throw data
  }
},

*create ({ payload }, { call, put }) {
  debugger
  const data = yield call(create, payload)
  if (data.success) {
    yield put({ type: 'hideModal' })
    yield put({ type: 'query' })
  } else {
    throw data
  }
},

*update ({ payload }, { select, call, put }) {
  const id = yield select(({ autoreply }) => autoreply.currentItem.id)
  const newAutoreply = { ...payload, id }
  const data = yield call(update, newAutoreply)
  if (data.success) {
    yield put({ type: 'hideModal' })
    yield put({ type: 'query' })
  } else {
    throw data
  }
},

},

reducers: {

  showModal (state, { payload }) {
    return { ...state, ...payload, modalVisible: true }
  },

  hideModal (state) {
    return { ...state, modalVisible: false }
  },

  switchIsMotion (state) {
    localStorage.setItem(`${prefix}autoreplyIsMotion`, !state.isMotion)
    return { ...state, isMotion: !state.isMotion }
  },

},
})
